<?php

//TODO quality is sooo bad
class ImgResizer{
	private $srcImg, $srcImgX, $srcImgY;
	private $srcDir, $folderName;


	function __construct(){
	}

	public function setSourceImg($img){
		$this->srcImg = imagecreatefromjpeg($img);
		$size = getimagesize($img);
		if($size[0]<$size[1]){
			$temp = $size[0];
			$size[0] = $size[1];
			$size[1] = $temp;
		}
		$this->srcImgX = $size[0];
		$this->srcImgY = $size[1];
	}

	public function setSourceFolder($folderName){
		$this->folderName = $folderName;
		$this->srcDir = opendir($folderName);
	}

	public function resizeImg($newName, $x, $y){
		$dstImg = imagecreatetruecolor($x, $y);
		imagecopyresampled($dstImg, $this->srcImg, 0, 0, 0, 0, $x, $y, $this->srcImgX, $this->srcImgY);
		imagejpeg($dstImg, $newName.".jpg");
		imagedestroy($dstImg);
	}

	public function resizeFolder($newName, $x, $y){
		$nameCounter = 0;
			if (!file_exists ($newName))
		{
			echo "existiert nicht";
			echo mkdir($newName);
		}else{
			echo !file_exists ($newName);
			echo $newName." existiert.";
		}
		
		echo "Start...<br />";

		while($foundFile = readdir($this->srcDir)) {

			if($foundFile != "." && $foundFile != ".." ){
				$this->srcImg = imagecreatefromjpeg($this->folderName."/".$foundFile);
				var_dump($this->srcImg);
				$size = getimagesize($this->folderName."/".$foundFile);
				$this->srcImgX = $size[0];
				$this->srcImgY = $size[1];
				$this->resizeImg($newName.$nameCounter,$x,$y);
				echo "Renaming ".$foundFile." to ".$newName.$nameCounter."...<br />";

				$nameCounter++;

			}
		}
		echo "Ende...<br />";

	}
}